package manage.controller;	
	
import com.github.pagehelper.PageInfo;
import manage.dao.UserMapper;
import manage.model.*;
import manage.service.impl.PayServiceImpl;
import manage.service.impl.RatesServiceImpl;
import manage.service.impl.WageServiceImpl;
import manage.util.LogUtil;
import manage.util.Msg;
import manage.util.PayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;	
import org.springframework.web.bind.annotation.RequestMethod;	
import org.springframework.web.bind.annotation.RequestParam;	
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;

import manage.model.Wage;
import manage.service.WageService;
import manage.base.BaseController;

import java.io.InputStream;
import java.util.*;
	
@Controller	
@RequestMapping("/")	
public class WageController extends BaseController<Wage,WageService<Wage>> {	
		
	@Autowired	
	private WageServiceImpl wageService;
	@Autowired
	private PayServiceImpl payService;
	@Autowired
	private  RatesServiceImpl ratesService;

	@Autowired
	private UserMapper userMapper;

	private User user;
	//返回Wage的list
	@RequestMapping("/wage")
	@ResponseBody
	public Msg WageData(HttpServletRequest request, Authentication authentication){
		String page = request.getParameter("currentPage");
		String limit = request.getParameter("pageSize");
		UserDetails userDetails =(UserDetails) authentication.getPrincipal();
		user = userMapper.selectByPrimaryKey(userDetails.getUsername());
		return Msg.success().add("data",wageService.selectAll(page,limit));
	}

	//添加Wage
	@ResponseBody
	@RequestMapping(value = "/addWage",method = RequestMethod.POST)
	public Msg addWage(Wage obj){
		Pay pay = new Pay();
		pay.setAccount(obj.getAccount());
		pay.setUsername(obj.getUsername());
		double base = Integer.parseInt(obj.getBsalary())+Integer.parseInt(obj.getPremium())
				+obj.getAllowanceforhightemperature()-obj.getEarlyretirementpenalty()
				+obj.getOvertimereward()+obj.getPerformancereward()
				-obj.getTardinesspenalty()+obj.getRentalsubsidy()+obj.getTrafficsubsidy();
		pay.setBasewage(base+"");
		double pay01 = base-base*PayUtil.readProperty2()/100;
		pay01 = (double) Math.round(pay01 * 100) / 100;
		pay.setPaywage(pay01+"");
		pay.setBooleanpay("否");
		pay.setPaytime(new Date());
		pay.setAt(ratesService.getMoney((int)pay01));
		payService.insert(pay);
		String s= wageService.insert(obj);
		LogUtil.writeFileLog(user.getAccount(),"添加文件类型:"+obj.toString());
		return Msg.attendanceAdd(s);
	}

	//修改Wage
	@ResponseBody
	@RequestMapping(value = "/addWage",method = RequestMethod.PUT)
	public Msg updateWage(Wage obj){
		Pay pay = payService.select(obj.getId());
		pay.setAccount(obj.getAccount());
		pay.setUsername(obj.getUsername());
		double base = Integer.parseInt(obj.getBsalary())+Integer.parseInt(obj.getPremium())
				+obj.getAllowanceforhightemperature()-obj.getEarlyretirementpenalty()
				+obj.getOvertimereward()+obj.getPerformancereward()
				-obj.getTardinesspenalty()+obj.getRentalsubsidy()+obj.getTrafficsubsidy();
		pay.setBasewage(base+"");
		double pay01 = base-base*PayUtil.readProperty2()/100;
		pay.setPaywage(pay01+"");
		pay.setBooleanpay("否");
		pay.setPaytime(new Date());
		pay.setAt(ratesService.getMoney((int)pay01));
		payService.update(pay);
		String s= wageService.update(obj);
		LogUtil.writeFileLog(user.getAccount(),"修改文件类型-改:"+obj.toString());
		return Msg.attendanceAdd(s);
	}
	//删除Wage
	@ResponseBody
	@RequestMapping("/delWage")
	public Msg delWage(@RequestBody Integer id){
		String s = wageService.del(id);
		payService.del(id);
		LogUtil.writeFileLog(user.getAccount(),"删除文件类型:"+id.toString());
		return Msg.attendanceAdd(s);
	}

	//根据主键查Wage 和修改配合使用
	@ResponseBody
	@RequestMapping("/wageSelect")
	public Msg selWage(@RequestBody Integer id){
		Wage obj = wageService.select(id);
		LogUtil.writeFileLog(user.getAccount(),"修改文件类型-查:"+obj.toString());
		return Msg.success().add("wage",obj);
	}

	@ResponseBody
	@RequestMapping(value = "/importShort")
	public String InputExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
		String flag = "02";// 上传标志
		if (!file.isEmpty()) {
			try {
				String originalFilename = file.getOriginalFilename();// 原文件名字
				//log.info("文件名：" + originalFilename);
				InputStream is = file.getInputStream();// 获取输入流
				flag = wageService.InputExcel(is, originalFilename);
			} catch (Exception e) {
				flag = "03";// 上传出错
				e.printStackTrace();
			}
		}
		return flag;
	}



	//返回Wage的list
	@RequestMapping("//wagePerson")
	@ResponseBody
	public Msg WageDataPerson(HttpServletRequest request, Authentication authentication){
		String page = request.getParameter("currentPage");
		String limit = request.getParameter("pageSize");
		UserDetails userDetails =(UserDetails) authentication.getPrincipal();
		user = userMapper.selectByPrimaryKey(userDetails.getUsername());
		List<Pay>pays=payService.selectByAccount(user.getAccount());
		com.github.pagehelper.PageInfo<Pay> pageInfo = new PageInfo<Pay>(pays);

		return Msg.success().add("data",pageInfo);
	}
}	
